package com.cowain.monitor.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.cowain.monitor.dao.ErpCustomerDao;
import com.cowain.monitor.dao.PmsClientDao;
import com.cowain.monitor.datasources.DataSourceNames;
import com.cowain.monitor.datasources.annotation.DataSource;
import com.cowain.monitor.entity.ErpCustomerEntity;
import com.cowain.monitor.entity.ErpSofEntity;
import com.cowain.monitor.entity.PmsClientEntity;
import com.cowain.monitor.service.ErpCustomerService;
import com.cowain.monitor.utils.PmsConstantUtils;
import com.cowain.monitor.utils.PmsProjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 *  
 *  * @author 冬暖
 *  * @projectName pms-monitor
 *  * @title     ErpCustomerServiceImpl   
 *  * @package    com.cowain.monitor.service.impl  
 *  * @description    客户信息表       
 *  * @date   2020/8/5 16:23  
 *  * @version V1.0.0
 *  
 */
@Service
public class ErpCustomerServiceImpl extends ServiceImpl<ErpCustomerDao,ErpCustomerEntity> implements ErpCustomerService {
    @Autowired
    private PmsProjectUtil pmsProjectUtil;
    @Autowired
    private PmsClientDao clientDao;
    /**
     * 获取所有的客户
     */
    @Override
    @DataSource(name = DataSourceNames.SECOND)
    public List<ErpCustomerEntity> findCustomerList(){
        return baseMapper.findCustomerList(PmsConstantUtils.IS_DEL);
    }

    /**
     * 插入数据
     */
    @Override
    @DataSource(name = DataSourceNames.FIRST)
    public void insertClient(List<ErpCustomerEntity> erpCustomerEntities){
        List<PmsClientEntity> infoList = new ArrayList<>();
        List<PmsClientEntity> infoEntityList = new ArrayList<>();
        erpCustomerEntities.forEach(client -> {
            PmsClientEntity clientEntity = new PmsClientEntity();
            Long clientNumber;
            if(client.getErpCustomerSiteEntity() != null && client.getErpCustomerSiteEntity().size() > 0){
                clientNumber  = clientDao.findCustomer(client.getCustId(),client.getErpCustomerSiteEntity().get(0).getCustAddress());
                clientEntity.setClientAddress(client.getErpCustomerSiteEntity().get(0).getCustAddress());
                clientEntity.setClientContact(client.getErpCustomerSiteEntity().get(0).getCustPhonePerson());
                clientEntity.setContactPhone(client.getErpCustomerSiteEntity().get(0).getCustPhoneNum());
            }else {
                clientNumber = clientDao.findClient(client.getCustId());
                clientEntity.setClientAddress(null);
                clientEntity.setClientContact(null);
                clientEntity.setContactPhone(null);
            }
            clientEntity.setClientName(client.getCustName());
            clientEntity.setClientType(client.getCustCategoryName());
            clientEntity.setClientIntension("客户意图");
            clientEntity.setId(client.getCustId());
            clientEntity.setReference(null);
            clientEntity.setInsertTime(new Date());
            clientEntity.setIndustry(client.getTradeCategory());
            clientEntity.setInsertUser(client.getOperaName());
            if(clientNumber != null){
                infoEntityList.add(clientEntity);
            }else {
                infoList.add(clientEntity);
            }
        });
        List<PmsClientEntity> list1 = new ArrayList<>();
        boolean flag = false;
        List<Long> list = pmsProjectUtil.addClientList(infoList, list1);
        if(infoList.size() > 0 && list.size() > 0){
            List<PmsClientEntity> list2 = new ArrayList<>();
            infoList.forEach(info -> {
                if(info.getId() == null){
                    list2.add(info);
                }
            });
            infoList.removeAll(list2);
            if(infoList.size() > 0){
                List<List<PmsClientEntity>> split = pmsProjectUtil.split(infoList, 12);
                for (List<PmsClientEntity> clientEntities : split) {
                    clientDao.insertList(clientEntities);
                }
                flag = true;
            }
        }
        if(infoEntityList.size() > 0){
            if(list1.size() > 0){
                infoEntityList.addAll(list1);
            }
            List<List<PmsClientEntity>> split = pmsProjectUtil.split(infoEntityList, 12);
            for (List<PmsClientEntity> pmsClientEntities : split) {
                clientDao.updateInfoList(pmsClientEntities);
            }
        }
        if(list1.size() ==0){
            list1.addAll(infoEntityList);
        }
        if(list.size() > 0 || list1.size() > 0) {
            List<PmsClientEntity> pmsClientEntities = new ArrayList<>();
            if(flag){
                pmsClientEntities.addAll(infoList);
            }
            pmsProjectUtil.addLog(pmsClientEntities, list1, PmsConstantUtils.ERP_USER, PmsConstantUtils.ERP_USER);
        }
    }

    /**
     * 修改状态
     */
    @Override
    @DataSource(name = DataSourceNames.SECOND)
    public void updateCustomer(List<ErpCustomerEntity> erpCustomerEntities){
        erpCustomerEntities.forEach(so -> {
            if(so.getCustId() != null){
                so.setIsDel(1);
                if(so.getErpCustomerSiteEntity().get(0) != null && so.getErpCustomerSiteEntity().get(0).getCustomer() != null){
                    so.getErpCustomerSiteEntity().get(0).setIsDel(1);
                }
            }else {
                so.setIsDel(0);
                if(so.getErpCustomerSiteEntity().get(0) != null){
                    so.getErpCustomerSiteEntity().get(0).setIsDel(0);
                }
            }
        });
        List<ErpCustomerEntity> collect = erpCustomerEntities.stream()
                .filter(s -> s.getIsDel() == 1)
                .collect(Collectors.toList());
        List<List<ErpCustomerEntity>> split = pmsProjectUtil.split(collect, 12);
        for (List<ErpCustomerEntity> customerEntities : split) {
            baseMapper.updateCustomer(customerEntities);
            customerEntities.forEach(es -> {
                if(es.getErpCustomerSiteEntity() != null && es.getErpCustomerSiteEntity().size() > 0){
                    baseMapper.updateCustomerSite(es.getErpCustomerSiteEntity());
                }
            });
        }
    }
}
